/*************************************************************************
	> File Name: B3730.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2025-07-09 16:46:30
 ************************************************************************/

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

#define MAX_N 99999
int prime[MAX_N + 5];
void init_prime() {
    for (int i = 2; i <= MAX_N; i++) {
        if (prime[i]) continue;
        prime[++prime[0]] = i;
        for (int j = i, I = MAX_N / i; j <= I; j++) {
            prime[j * i] = 1;
        }
    }
    return ;
}

int calc(const string &a, const string &b) {
    int cnt = 0;
    for (int i = 0; i < a.size(); i++) {
        if (a[i] == b[i]) continue;
        int x = a[i] - '0';
        int y = b[i] - '0';
        int res = abs(x - y);
        cnt += min(res, 10 - res);
    }
    return cnt;
}

int main() {
    init_prime();
    string s;
    cin >> s;
    int min_cnt = 50;
    string ans = s;
    for (int i = prime[0]; i >= 1; i--) {
        string val = to_string(prime[i]);
        while (val.size() < 5) val = '0' + val;
        int cnt = calc(s, val);
        if (cnt < min_cnt) {
            min_cnt = cnt;
            ans = val;
        }
    }
    cout << ans << endl;
    return 0;
}
